package com.thoughtworks.retailstore.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.thoughtworks.models.CartModel;
import com.thoughtworks.retailstore.R;
import com.thoughtworks.retailstore.adapters.CartListAdapter;
import com.thoughtworks.retailstore.utils.Constants;

// TODO: Auto-generated Javadoc
/**
 * The Class CartDetailsFragment.
 */
public class CartDetailsFragment extends Fragment 
{

	/** The list view. */
	private ListView listView ;

	/** The cart. */
	private static CartModel cart ;

	/** The footer view. */
	private View footerView ;

	/** The total price text view. */
	private TextView totalPriceTextView ;

	/**
	 * New instance.
	 *
	 * @return the cart details fragment
	 */
	public static CartDetailsFragment newInstance() //CartModel _cart
	{
		CartDetailsFragment f = new CartDetailsFragment();
		cart = CartModel.getInstance() ;
		return f ;
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.cart_container_layout, null);
		footerView = inflater.inflate(R.layout.cart_footer_view, null);
		initViews(view);
		setData();
		
		
		return view;
	}

	/**
	 * Inits the views.
	 *
	 * @param view the view
	 */
	public void	initViews(View view)
	{
		try{
			listView = (ListView)view.findViewById(R.id.cartListView);
			Constants.cartList.add(cart);
			totalPriceTextView = (TextView)footerView.findViewById(R.id.totalPriceTv);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * Sets the data.
	 */
	public void setData()
	{
		try{
			CartListAdapter adapter = new CartListAdapter(getActivity(), 
					cart, totalPriceTextView);
			listView.addFooterView(footerView);

			totalPriceTextView.setText(Integer.toString(cart.cartTotal())+" INR");

			listView.setAdapter(adapter);
		}
		catch(Exception e){
			e.printStackTrace();
		}

	}
}
